package com.shinhye.smartsms;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.shinhye.smartsms.utils.CommonUtils;

public class CreateSMSActivity extends Activity implements OnClickListener{
	private AutoCompleteTextView actv_sendto;
	private EditText et_sendcontent;
	private CursorAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_sms);
		
		actv_sendto = (AutoCompleteTextView) findViewById(R.id.actv_sendto);
		et_sendcontent = (EditText) findViewById(R.id.et_sendcontent);
		ImageButton ib_select_contact = (ImageButton) findViewById(R.id.ib_select_contact);
		Button btn_newsms_send = (Button) findViewById(R.id.btn_newsms_send);
		
		ib_select_contact.setOnClickListener(this);
		btn_newsms_send.setOnClickListener(this);
		
		adapter = new MyCursorAdapter(this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		adapter.setFilterQueryProvider(new MyFilterQueryProvider());
		actv_sendto.setAdapter(adapter);
		actv_sendto.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				et_sendcontent.requestFocus();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.ib_select_contact:
			Intent intent = new Intent(Intent.ACTION_PICK);
			intent.setData(Uri.parse("content://com.android.contacts/contacts"));
			startActivityForResult(intent, 120);
			break;
		case R.id.btn_newsms_send:
			String address = actv_sendto.getText().toString();
			if(TextUtils.isEmpty(address)) {
				Toast.makeText(this, "电话号码不能为空！", Toast.LENGTH_SHORT).show();
				break;
			}
			String text = et_sendcontent.getText().toString();
			CommonUtils.sendSMS(this, address, text);
			finish();
			break;
		}
	}
	
	class MyFilterQueryProvider implements FilterQueryProvider{
		@Override
		public Cursor runQuery(CharSequence constraint) {
			System.out.println("constraint---" + constraint);
			
			String[] projection = {
					"_id", "display_name_alt", "data1"
			};
			Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, "data1 like ?", new String[]{constraint + "%"}, projection[0]);
			CommonUtils.printCursor(cursor);
			
			return cursor;
		}
	}
	
	class MyCursorAdapter extends CursorAdapter {
		public MyCursorAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		@Override
		//必须重写该方法，保证点击Item后可以返回制定数据
		public CharSequence convertToString(Cursor cursor) {
			return cursor.getString(2);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = View.inflate(context, R.layout.item_contact_autocomplete, null);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
			TextView tv_address = (TextView) view.findViewById(R.id.tv_address);
			tv_name.setText(cursor.getString(1));
			tv_address.setText(cursor.getString(2));
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 120) {
			if(data == null)
				return;
			Uri uri = data.getData();
			int _id = CommonUtils.getContactByUri(getContentResolver(), uri);
			if(_id == -1) {
				Toast.makeText(this, "该联系人没有号码！", Toast.LENGTH_SHORT).show();
			} else {
				String address = CommonUtils.getAddressById(getContentResolver(), _id);
				actv_sendto.setText(address);
				et_sendcontent.requestFocus();
			}
		}
	}
}
